package com.slwish.xty.ui.activity;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.slwish.common.base.BaseActivity;
import com.slwish.common.config.Contants;
import com.slwish.common.utils.DensityUtils;
import com.slwish.common.utils.SpUtils;
import com.slwish.xty.R;
import com.slwish.xty.adapter.GuidePageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SplishActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @BindView(R.id.guide_ll_point)
    public ViewGroup vg;//放置圆点
    @BindView(R.id.guide_vp)
    public ViewPager vp;

    private int[] imageIdArray;//图片资源的数组
    private List<View> viewList;//图片资源的集合
    //实例化原点View
    private ImageView iv_point;
    private ImageView[] ivPointArray;

    //最后一页的按钮
    @BindView(R.id.guide_ib_start)
    public TextView ib_start;

    @Override
    protected void initBefore() {
        super.initBefore();
//        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_splish;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        //加载ViewPager
        initViewPager();
        //加载底部圆点
        initPoint();
    }


    /**
     * 加载图片ViewPager
     */
    private void initViewPager() {
        //实例化图片资源
        imageIdArray = new int[]{R.mipmap.img_splish1, R.mipmap.img_splish2, R.mipmap.img_splish3};
        viewList = new ArrayList<>();
        //获取一个Layout参数，设置为全屏
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        //循环创建View并加入到集合中
        int len = imageIdArray.length;
        for (int i = 0; i < len; i++) {
            //new ImageView并设置全屏和图片资源
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundResource(imageIdArray[i]);

            //将ImageView加入到集合中
            viewList.add(imageView);
        }

        //View集合初始化好后，设置Adapter
        vp.setAdapter(new GuidePageAdapter(viewList));
        //设置滑动监听
        vp.addOnPageChangeListener(this);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * 加载底部圆点
     */
    private void initPoint() {
        //这里实例化LinearLayout
        //根据ViewPager的item数量实例化数组
        ivPointArray = new ImageView[viewList.size()];
        //循环新建底部圆点ImageView，将生成的ImageView保存到数组中
        int size = viewList.size();
        int px2dp14 = DensityUtils.dp2px(mContext, 7);
        int px2dp20 = DensityUtils.dp2px(mContext, 9);
        for (int i = 0; i < size; i++) {
            iv_point = new ImageView(mContext);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(px2dp14, px2dp14);
            if (i != 0) layoutParams.leftMargin = px2dp20;
            iv_point.setLayoutParams(layoutParams);
            iv_point.setPadding(px2dp20, 0, 0, 0);//left,top,right,bottom

            ivPointArray[i] = iv_point;
            //第一个页面需要设置为选中状态，这里采用两张不同的图片
            if (i == 0) {
                iv_point.setBackgroundResource(R.mipmap.oval_green);
            } else {
                iv_point.setBackgroundResource(R.mipmap.oval);
            }
            //将数组中的ImageView加入到ViewGroup
            vg.addView(ivPointArray[i]);
        }


    }

    /**
     * 滑动后的监听
     *
     * @param position
     */
    @Override
    public void onPageSelected(int position) {
        //循环设置当前页的标记图
        int length = imageIdArray.length;
        for (int i = 0; i < length; i++) {
            ivPointArray[position].setBackgroundResource(R.mipmap.oval_green);
            if (position != i) {
                ivPointArray[i].setBackgroundResource(R.mipmap.oval);
            }
        }

        //判断是否是最后一页，若是则显示按钮
        if (position == length - 1) {
            ib_start.setVisibility(View.VISIBLE);
        } else {
            ib_start.setVisibility(View.GONE);
        }
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @OnClick(R.id.guide_ib_start)
    public void setOnClick(View view) {
        SpUtils.put(mContext, Contants.FIRST_OPEN, false);
        startActivity(new Intent(mContext, MainActivity.class));
        finish();


    }
}
